<?php
namespace app\manage\controller;

use think\Controller;
use think\Request;
use think\Validate;
use think\Db;
use Config;
use app\common\model\TokenCheck;

/**住房房源设置
 * Class UnitBuilding
 * @package app\manage\controller
 */
class HousingInformation extends Controller{
	/*public function __construct()
	{
		$this->token_check = new TokenCheck();
	}*/
    /**
     * 住房房源显示
     * @return \think\response\Json
     */
    public function HousingList(){
        $model=new \app\manage\model\HousingInformation();
        $date=$model->index();
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 住房房源删除
     * @return \think\response\Json
     */
    public function HousingDel(){
        $model=new \app\manage\model\HousingInformation();
        $data = input();
        $date=$model->housingDel($data);
        returnJson(200,"成功")->send();die;
    }
    /*
     * 上传图片
     */
    public function  appImg(Request $request){
        $file=$request->file('img');
        if(empty($file)){$arr = ["code" => -401, "msg" => "请选择图片",];return json($arr);}
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // /public/uploads/20190422\c6f72ef843d6cb9d59d831ba6ae49660.png,存入数据库转义了
            $newPath=str_replace('\\','/',"/uploads/".$info->getSaveName());
            $arr = ["code" => 200, "data" => $newPath,];return json($arr);
        }
        else{echo  json_encode(["code" =>'10003','msg'=>$file->getError()], JSON_UNESCAPED_UNICODE);exit;}
    }

    /**
     * 住房房源添加
     * @return \think\response\Json
     */
	public function HousingAdd(){
        $model=new \app\manage\model\HousingInformation();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $matching=[];
            $data=$model->housingList($matching);
            //判断后台有没有设置单元楼栋数
            if($data==2){
                returnJson(-411,"请先在公寓设置里面设置")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['h_tung_num', 'require|number', '请选择栋数|栋数必须是数字'],
                ['h_unit_num', 'require|number', '请选择单元|单元必须是数字'],
                ['h_layer_num', 'require|number', '请填写层数|层数必须是数字'],
                ['h_room_number', 'require|number', '请填写房号|房号必须是数字'],
                ['h_area', 'require|number', '请填写面积|单面积必须是数字'],
                ['h_root', 'require|number', '请选择室|室必须是数字'],
                ['h_guest_room', 'require|number', '请选择厅|厅必须是数字'],
                ['h_toilet', 'require|number', '请选择卫|卫必须是数字']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断楼层房号是否重复
            $i=$model->tungs($data);
            if($i!=null){
                return json(["code"=>-416,"msg"=>'该单元楼层房号已存在']);
            }
            $date=$model->housingAdd($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"添加失败")->send();die;
            }
        }
	}

    /**
     * 住房房源修改
     * @return \think\response\Json
     */
    public function HousingUpdate(){
        $model=new \app\manage\model\HousingInformation();
        //get的时候显示需要修改的单元楼和单元楼post的时候修改
        if(Request::instance()->isGet()){
            $input = input();
            $h_id = trim( $input['housing_id'] );
            $data=$model->housingOne($h_id);
            $data['setting']=$model->housingList($data['matching']);

            //判断后台有没有设置单元楼栋数
            if($data==null){
                returnJson(-413,"请选择正确的住房")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['h_tung_num', 'require|number', '请选择栋数|栋数必须是数字'],
                ['h_unit_num', 'require|number', '请选择单元|单元必须是数字'],
                ['h_layer_num', 'require|number', '请填写层数|层数必须是数字'],
                ['h_room_number', 'require|number', '请填写房号|房号必须是数字'],
                ['h_area', 'require|number', '请填写面积|单面积必须是数字'],
                ['h_root', 'require|number', '请选择室|室必须是数字'],
                ['h_guest_room', 'require|number', '请选择厅|厅必须是数字'],
                ['h_toilet', 'require|number', '请选择卫|卫必须是数字']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断楼层房号是否重复
            $i=$model->tungsUp($data);
            if($i!=null){
                returnJson(-416,"该单元楼层房号已存在")->send();die;
            }
            $date=$model->unitUpdate($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-414,"修改失败")->send();die;
            }
        }
    }

    /*
    * 上传图片
    */
    public function  image(Request $request){
        $file=$request->file('img');
        if(empty($file)){$arr = ["code" => -401, "msg" => "请选择图片",];return json($arr);}
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // /public/uploads/20190422\c6f72ef843d6cb9d59d831ba6ae49660.png,存入数据库转义了
            $newPath=str_replace('\\','/',"/uploads/".$info->getSaveName());
            $arr = ["code" => 200, "data" => $newPath,];return json($arr);
        }
        else{echo  json_encode(["code" =>'10003','msg'=>$file->getError()], JSON_UNESCAPED_UNICODE);exit;}
    }

}